package code

import (
	"fmt"
	"time"
)

func Time01() {
	now := time.Now()
	fmt.Println("now=", now)
	// 格式化
	fmt.Println("格式化后: ", now.Format("2006-01-02 15:04:05.000"))
	fmt.Println("格式化后: ", now.Format("2006年01月02日 15时04分05.000"))

	milli := now.UnixMilli()
	fmt.Println("毫秒=", milli)
	second := now.Unix()
	fmt.Println("秒=", second)
	time.Sleep(5 * time.Second)

	// 20秒之后
	after20 := now.Add(20 * time.Second)
	fmt.Printf("now after 20 seconds: %s\n", after20.Format("2006-01-02 15:04:05.000"))
	sub := after20.Sub(now)
	fmt.Printf("between now and after20: %v\n", sub)
}

func Time02() {
	ch := make(chan struct{})
	go wait(ch)
	select {
	case <-ch:
		fmt.Println("任务完成")
	case <-time.After(2 * time.Second):
		fmt.Println("timeOut")
	}
}

func wait(ch chan struct{}) {
	time.Sleep(3 * time.Second)
	ch <- struct{}{}
}
